package com.mo.core.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;

public class XMLSender {
	private static Log logger = LogFactory.getLog(XMLSender.class);
	
	public static String sendXml(String xmlStr, String postUrl) throws Exception{
		String returnStr = "";
		HttpClient httpclient = new HttpClient();
		httpclient.getParams().setConnectionManagerTimeout(10*1000);//10s
		httpclient.getParams().setSoTimeout(10*1000);//10s
		PostMethod post = new PostMethod(postUrl);
		//System.out.println(xmlStr);
		logger.info("request:" + xmlStr);
		logger.info("url: " + postUrl);
		post.setRequestEntity(new StringRequestEntity(xmlStr,"text/xml","utf-8"));
		int statusCode = httpclient.executeMethod(post);
		if(statusCode == HttpStatus.SC_OK){
			InputStream is = post.getResponseBodyAsStream();
			returnStr = IOUtils.toString(is, "utf-8");
			is.close();
			logger.info(returnStr.substring(0, Math.min(returnStr.length(), 1000)));
		}
		post.releaseConnection();
		httpclient.getHttpConnectionManager().closeIdleConnections(0);
		return returnStr;
	}

	public static String get(String url) throws Exception{
		String returnStr = "";
		HttpClient httpclient=new HttpClient();
		httpclient.getParams().setConnectionManagerTimeout(10*1000);//10s
		httpclient.getParams().setSoTimeout(10*1000);//10s
		GetMethod get=new GetMethod(url);
		get.addRequestHeader("Content-type" , "text/html; charset=utf-8"); 
		logger.info("get url : " + url);
		int statusCode = httpclient.executeMethod(get);
		if(statusCode == HttpStatus.SC_OK){
			InputStream is = get.getResponseBodyAsStream();
			returnStr = IOUtils.toString(is, "utf-8");
			logger.info(returnStr.substring(0, Math.min(returnStr.length(), 1000)));
			is.close();
		}
		get.releaseConnection();
		httpclient.getHttpConnectionManager().closeIdleConnections(0);
		return returnStr.trim();
	}
	
	public static String post(String url,Map params) throws Exception{
		String returnStr = "";
		HttpClient httpclient = new HttpClient();
		httpclient.getParams().setConnectionManagerTimeout(10*1000);//10s
		httpclient.getParams().setSoTimeout(10*1000);//10s
		PostMethod post = new PostMethod(url);
		httpclient.getParams().setContentCharset("UTF-8");
		NameValuePair[] pairs =null;
		if(params!=null && params.size() > 0){
			pairs = new NameValuePair[params.size()];
			int i =0;
			for(Object key: params.keySet()){
				NameValuePair p = new NameValuePair();
				String k = (String) key;
				p.setName(k);
				p.setValue((String)params.get(k));
				pairs[i] = p;
				i++;
			}
			
		}else{
			pairs = new NameValuePair[0];
		}
		post.addParameters(pairs);
		//post.setRequestBody(pairs); //此时和上一句效果一样
		
		logger.info("url: " + url);
		int statusCode = httpclient.executeMethod(post);
		if(statusCode == HttpStatus.SC_OK){
			InputStream is = post.getResponseBodyAsStream();
			returnStr =  IOUtils.toString(is, "utf-8");
			is.close();
			logger.info(returnStr.substring(0, Math.min(returnStr.length(), 1000)));
		}
		post.releaseConnection();
		httpclient.getHttpConnectionManager().closeIdleConnections(0);
		return returnStr;
	}
	
	/*public static void main(String[] args){
		Map<String,String> params = new HashMap<String,String>();
		params.put("vo.keyword","D");
		params.put("vo.keytype", "10,11");
		params.put("vo.pageno","1");
		params.put("vo.pagesize","20");
		params.put("vo.pinyin","1");
		params.put("vo.providerIds","260201205,260201205");
		params.put("vo.plat","android");
		String xml = "";
		String url = "http://localhost:8780/search/data!search.action";
		try {
			String a = post(url, params);
//			SearchContentListVO jsonVo =(SearchContentListVO)JSONObject.toBean(JSONObject.fromObject(a), SearchContentListVO.class);
			//System.out.println(jsonVo.getContentList().size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}*/
}
